console.log('content script已启动！');

// 向background发送页面信息
function sendPageInfo(title, price, salesCount, shopname,comment) {
  console.log('开始发送页面信息：', title, price, salesCount, shopname,comment);
  chrome.runtime.sendMessage({action: 'sendPageInfo', title: title, price: price, salesCount: salesCount,shopname:shopname ,comment:comment});
}

// 获取飞猪页面信息
function getfliggyPageInfo() {
    setTimeout(function(){
    var title = document.querySelectorAll('.title-txt > span')[0]?.innerText;
    var price = document.querySelector('.big-price span').innerText;
    var salesCount = document.querySelector('.item-data:nth-of-type(1)').innerText;
    var shopname = document.querySelector('.c-shop-logo-name').innerText;
    window.onload = function() {
        // 在页面加载完成后获取DOM元素
        var salesCount = document.querySelector('.item-data:nth-of-type(1)').innerText;
      };
      var dom=document.querySelector('.info-list').childNodes;
     salesCount = dom[0]?.firstElementChild?.innerText;
     var comment = dom[1]?.firstElementChild?.innerText;
    console.log('获取页面信息成功：', title, price, salesCount, shopname);
    sendPageInfo(title, price, salesCount, shopname,comment);
    },1000)
    
  }
// 监听background发来的消息
chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) {
  if (request.action === 'getfliggyPageInfo') {
    getfliggyPageInfo()
  }
});